package com.teapos.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teapos.dao.OrderDao;
import com.teapos.enumtype.TradeTypeEnum;
import com.teapos.util.ThreadLocalUtil;
import com.teapos.util.Tool;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class SummaryService {
	
	@Autowired
	private OrderDao orderDao;

	public List getSummary(Map map) {
		Tool.initPage(map);
		beforeQuery(map);
		List orders = orderDao.summaryOrders(map);
		Iterator iter = orders.iterator();
		int sum = 0;
		int sum2 = 0;
		while(iter.hasNext()) {
			Map order = (Map)iter.next();
			order.put("order_date_display", Tool.formatDate((Date)order.get("order_date"), "yyyy-MM-dd HH:mm"));
			TradeTypeEnum enumtype = TradeTypeEnum.getEnumByCode(Tool.toInt(order.get("type")));
			order.put("type_display", enumtype==null?"--":enumtype.getDesc());
			sum+=Tool.toDouble(order.get("org_price"));
			sum2+=Tool.toDouble(order.get("now_price"));
		}
		//System.err.println(sum);
		ThreadLocalUtil.put("sum", sum);
		ThreadLocalUtil.put("sum2", sum2);
		return orders;
	}
	
	public int getCount(Map map) {
		beforeQuery(map);
		return orderDao.summaryOrdersCount(map);
	}
	
	private void beforeQuery(Map map) {
		String productName = Tool.toString(map.get("product_name"));
		if(StringUtils.isNotBlank(productName)) {
			map.put("product_name", "%"+productName+"%");
		}
	}

}
